﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Yto.Common.Dependency;
using Yto.Common.Linq;
using Yto.Repositories;

namespace com.ytocargo.Core.FlightPlanMakeLogs
{
    public class FlightPlanMakeLogStore : IFlightPlanMakeLogStore, ITransient
    {
        private readonly IBaseRepository<FlightPlanMakeLog> _rep;
        public IQueryableAsync queryableAsync { get; set; }
        public FlightPlanMakeLogStore(IBaseRepository<FlightPlanMakeLog> repository)
        {
            _rep = repository;
            queryableAsync = QueryableAsync.Instance;
        }

        public IQueryable<FlightPlanMakeLog> GetAll()
        {
            return _rep.GetAll();
        }

        public async Task<List<FlightPlanMakeLog>> GetUnSengdListAsync(int maxCount = 0)
        {
            return await _rep.GetListAsync(x => !x.SendFlag, maxCount);
        }

        public Task<List<FlightPlanMakeLog>> GetUnSengdListAsync(Expression<Func<FlightPlanMakeLog, bool>> expression, int maxCount = 0)
        {
            var queryable = _rep.GetAll().Where(expression);

            if (maxCount > 0)
            {
                queryable = queryable.Take(maxCount);
            }

            return queryableAsync.ToListAsync(queryable);
        }

        public async Task<FlightPlanMakeLog> UpdateFlightPlanMakeLogAsync(FlightPlanMakeLog flightPlanMakeLog)
        {
            return await _rep.UpdateAsync(flightPlanMakeLog);
        }
    }
}
